import React, { useContext, useEffect, useState } from 'react'
import './curriculums.less'
import { message, Spin } from 'antd';
import { Context } from '@/App';
import { addCurriculum, deleteCur, reqCurriculums, teaSearchCur, UpdateCur } from '@/api/api';
import { updateSearchInfor } from '@/tool/updateSearchInfor';
import useAdditionalInfor from '@/hooks/useAdditionalInfor';

import CurriculumList from '@/components/Curriculums/CurriculumsList';
import AddNewCurriculums from '@/components/Curriculums/AddNewCurriculums';
import CurriculumsSearch from '@/components/Curriculums/CurriculumsSearch'
import UpdateCurriculum from '@/components/Curriculums/UpdateCurriculum';

export default function Curriculum() {
  const [addOpen, setAddOpen] = useState(false);
  const [updateOpen, setUpdateOpen] = useState(false);
  const [loading, setLoading] = useState(true)

  const [curriculums, setCurriculums] = useState();
  const [searchInfor, setSearchInfor] = useState(null)
  const [updateInfor, setUpdateInfor] = useState(null)

  const { role } = useContext(Context)
  const { classes, teacher, allCurriculunms, professionals } = useAdditionalInfor(role)

  const additionalInfor = { classes, teacher, allCurriculunms, professionals }

  const getCurriculums = () => {
    setLoading(true)
    reqCurriculums().then(
      res => {
        if (res.status === 200) {
          setCurriculums(res.data)
          setLoading(false)
        }
      }
    )
  }

  useEffect(() => {
    if (role !== null) {
      getCurriculums()
    }
  }, [role])

  // 重置
  const Reset = () => {
    setSearchInfor(null)
    getCurriculums(role);
  }

  // 搜索
  const searchFinish = (data) => {
    const values = updateSearchInfor(data)
    setSearchInfor(values)
    teaSearchCur(values).then(
      res => {
        if (res.status === 200) {
          setCurriculums(res.data)
        }
        if (res.status === 201) {
          message.error("没有相应的课程")
        }
      }
    )
  };

  // 添加提交
  const addSubmit = (values) => {
    addCurriculum(values).then(
      res => {
        if (res.status === 200) {
          message.success(res.data)
        }
        if (res.status === 201) {
          message.error('添加失败')
        }
      }
    )
    setAddOpen(false);
  };
  // 修改提交
  const updateSubmit = (data) => {
    if (data.fd_teacNum !== role.fd_idNum) {
      message.error("不可修改不是自己所教的课程")
      return
    }
    console.log(data);
    UpdateCur(data).then(
      res => {
        if (res.status === 200) {
          setUpdateOpen(false)
          message.success(res.data)
          if (searchInfor !== null) {
            searchFinish(searchInfor)
          } else {
            getCurriculums()
          }
        }
      }
    )
  };

  // 修改的课程
  const updateCurriculum = (data) => {
    setUpdateInfor(data);
    setUpdateOpen(true)
  }

  // 删除课程
  const deleCurriculum = (data) => {
    if (data.fd_teacNum !== role.fd_idNum) {
      message.error("不可删除不是自己所教的课程")
      return
    }
    const { fd_curriculumNum, fd_teacNum, fd_professionalNum, fd_claNum } = data
    const deleteInfor = { fd_curriculumNum, fd_teacNum, fd_professionalNum, fd_claNum }
    deleteCur(deleteInfor).then(
      res => {
        if (res.status === 200) {
          message.success(res.data)
          if (searchInfor !== null) {
            searchFinish(searchInfor)
          } else {
            getCurriculums()
          }
        } else {
          message.error("删除失败！！！")
        }
      }
    )
  }

  return (
    <div className='curriculum-box'>
      {
        loading ? <Spin size='large' className='loading' /> :
          <>
            <div className="curriculum-select">
              <CurriculumsSearch
                onFinish={searchFinish}
                setAddOpen={setAddOpen}
                additionalInfor={additionalInfor}
                Reset={Reset}
              />
            </div>
            <div className='curriculum-table'>
              <CurriculumList
                curriculums={curriculums}
                updateCurriculum={updateCurriculum}
                deleCurriculum={deleCurriculum}
              />
            </div>
          </>
      }
      <AddNewCurriculums
        open={addOpen}
        onCreate={addSubmit}
        additionalInfor={additionalInfor}
        role={role}
        onCancel={() => {
          setAddOpen(false);
        }}
      />
      <UpdateCurriculum
        updateInfor={updateInfor}
        open={updateOpen}
        additionalInfor={additionalInfor}
        onCreate={updateSubmit}
        onCancel={() => {
          setUpdateOpen(false);
        }}
      />
    </div>
  )
}
